﻿using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace DYSales.Views.sell
{
    public partial class cwin_expAdd : ChildWindow
    {
        private Web.ExpenditureTable currentItem;

        internal Web.ExpenditureTable CurrentItem
        {
            get { return currentItem; }
            set
            {
                currentItem = value;
                LayoutRoot.DataContext = currentItem;
            }
        }

        public cwin_expAdd()
        {
            InitializeComponent();
            tb_分类.Populating += new PopulatingEventHandler(tb_分类_Populating);
            tb_分类.TextChanged += new RoutedEventHandler(tb_分类_TextChanged);
        }

        void tb_分类_TextChanged(object sender, RoutedEventArgs e)
        {
            CurrentItem.exp_类别 = tb_分类.Text;
        }

        void tb_分类_Populating(object sender, PopulatingEventArgs e)
        {
            AutoCompleteBox source = (AutoCompleteBox)sender;
            dm.getDS().GetExtFilter(e.Parameter, lo =>
            {
                if (!lo.HasError)
                {
                    var pds = new List<Web.ExpenditureTable>(lo.Value);
                    source.DataContext = from p in pds
                                         select new autoExt
                                         {
                                             expName = p.exp_类别,
                                             ext = p
                                         };
                    source.PopulateComplete();
                }
            }, null);
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            tbk_提示.Text = string.Empty;
            if (CurrentItem.exp_类别 != null)
            {
                if (CurrentItem.exp_支出金额 > 0)
                {
                    this.DialogResult = true;
                }
                else
                {
                    tbk_提示.Text = "支出金额不能是零";
                }
            }
            else
            {
                tbk_提示.Text = "请选择支出分类";
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
    public class autoExt
    {
        public string expName { get; set; }
        public Web.ExpenditureTable ext { get; set; }

        public override string ToString() { return expName; }
    }
}

